import SwiftUI

/// 实时折线图组件 - 显示传感器数据的实时波形
struct RealtimeChartView: View {
    let title: String
    let icon: String
    let color: Color
    let xData: [Double]
    let yData: [Double]
    let zData: [Double]
    let maxDataPoints: Int = 50 // 显示最近50个数据点
    
    var body: some View {
        VStack(spacing: 12) {
            // 标题
            HStack(spacing: 8) {
                Image(systemName: icon)
                    .font(.system(size: 14, weight: .semibold))
                    .foregroundColor(color)
                Text(title)
                    .font(.system(.caption, design: .rounded).weight(.semibold))
                    .foregroundColor(.white)
                Spacer()
            }
            
            // 三轴折线图
            VStack(spacing: 8) {
                ChartLine(data: xData, color: .red, label: "X", maxPoints: maxDataPoints)
                ChartLine(data: yData, color: .green, label: "Y", maxPoints: maxDataPoints)
                ChartLine(data: zData, color: .blue, label: "Z", maxPoints: maxDataPoints)
            }
            
            // 当前值显示
            HStack(spacing: 12) {
                AxisValue(label: "X", value: xData.last ?? 0, color: .red)
                AxisValue(label: "Y", value: yData.last ?? 0, color: .green)
                AxisValue(label: "Z", value: zData.last ?? 0, color: .blue)
            }
        }
        .padding(12)
        .background(.white.opacity(0.05))
        .cornerRadius(12)
    }
}

/// 单条折线图
private struct ChartLine: View {
    let data: [Double]
    let color: Color
    let label: String
    let maxPoints: Int
    
    private var displayData: [Double] {
        Array(data.suffix(maxPoints))
    }
    
    private var normalizedData: [CGFloat] {
        guard !displayData.isEmpty else { return [] }
        
        // 找到最大最小值用于归一化
        let maxVal = displayData.max() ?? 1.0
        let minVal = displayData.min() ?? -1.0
        let range = max(abs(maxVal), abs(minVal), 0.1) // 避免除以0
        
        // 归一化到 [-1, 1] 范围
        return displayData.map { CGFloat($0 / range) }
    }
    
    var body: some View {
        HStack(spacing: 4) {
            // 轴标签
            Text(label)
                .font(.system(.caption2, design: .rounded).weight(.bold))
                .foregroundColor(color)
                .frame(width: 12)
            
            // 折线图
            GeometryReader { geometry in
                Path { path in
                    guard normalizedData.count > 1 else { return }
                    
                    let width = geometry.size.width
                    let height = geometry.size.height
                    let stepX = width / CGFloat(max(normalizedData.count - 1, 1))
                    let centerY = height / 2
                    let scaleY = height / 2 * 0.8 // 留10%边距
                    
                    // 绘制折线
                    path.move(to: CGPoint(
                        x: 0,
                        y: centerY - normalizedData[0] * scaleY
                    ))
                    
                    for (index, value) in normalizedData.enumerated() where index > 0 {
                        path.addLine(to: CGPoint(
                            x: CGFloat(index) * stepX,
                            y: centerY - value * scaleY
                        ))
                    }
                }
                .stroke(color, lineWidth: 1.5)
                
                // 中心线
                Path { path in
                    let centerY = geometry.size.height / 2
                    path.move(to: CGPoint(x: 0, y: centerY))
                    path.addLine(to: CGPoint(x: geometry.size.width, y: centerY))
                }
                .stroke(Color.white.opacity(0.2), lineWidth: 0.5)
            }
            .frame(height: 30)
        }
    }
}

/// 轴数值显示
private struct AxisValue: View {
    let label: String
    let value: Double
    let color: Color
    
    var body: some View {
        VStack(spacing: 2) {
            Text(label)
                .font(.system(.caption2, design: .rounded).weight(.bold))
                .foregroundColor(color)
            Text(String(format: "%.2f", value))
                .font(.system(.caption2, design: .monospaced).weight(.medium))
                .foregroundColor(.white)
        }
        .frame(maxWidth: .infinity)
    }
}
